const verify = (rules: any, data: any): { status: boolean } => {
    const res: any = { status: true }
    const keys = Object.keys(rules)
    keys.forEach((item: any) => {
        const value = data[item]
        const arr = rules[item]
        if (value == undefined || value == null) {
            res.status = false
            res[item] = `${item} 属性不存在!`
        } else {
            for (let a = 0; a < arr.length; a++) {
                const { required, errorMessage, pattern, validateFunction } = arr[a]
                if (required && value.length <= 0) {
                    res.status = false
                    res[item] = errorMessage
                    break
                } else if (pattern && !pattern.test(value)) {
                    res.status = false
                    res[item] = errorMessage
                    break
                } else if (validateFunction) {
                    validateFunction(data[item], data, (msg: string) => {
                        res.status = false
                        res[item] = msg
                    })
                    break
                }
            }
        }
    })
    return res
}

export default verify
